﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECI.CommonLibrary;
using ECI.BaseLanguage;

namespace ECI.DynamicData
{
    /// <summary>
    /// 动态画面-创建编号
    /// </summary>
    public class DynamicNo
    {
        /// <summary>
        /// 创建编号
        /// </summary>
        /// <param name="expresstion">表达式</param>
        /// <param name="ts">事务</param>
        /// <returns></returns>
        public static string GetNo(string expresstion, object ts)
        {
            string primaryCreator = expresstion;
            string[] array = primaryCreator.Split(',');
            string key = "";
            if (array.Length == 1)
            {
                key = NoHelper.GetTableKey(primaryCreator, ts);
            }

            else
            {
                if (array.Length < 4) throw new Exception(Lang.A00064.Read());//主键配置的生成方式不正确!
                string type = array[0];
                string startNo = array[1];
                string length = array[2];
                string desc = array[3];

                startNo = startNo.Replace("{YYYYMMDD}", DateTime.Now.ToString("yyyyMMdd"));
                startNo = startNo.Replace("{YYYYMM}", DateTime.Now.ToString("yyyyMM"));
                startNo = startNo.Replace("{YYYY}", DateTime.Now.ToString("yyyy"));
                startNo = startNo.Replace("{YYMM}", DateTime.Now.ToString("yyMM"));
                startNo = startNo.Replace("{YY}", DateTime.Now.ToString("yyyy"));

                if (ECI.CoreLibrary.PlatformInfo.DevelopMode)
                {
                    ECI.CoreLibrary.LOG.Write(startNo, "自动产生编号");
                }

                int nLength = 0;
                if (!int.TryParse(length, out nLength)) throw new Exception(Lang.A00065.Read());//主键产生规则配置的流水码长度不是整数

                key = NoHelper.GetNo(type, startNo, int.Parse(length), desc, "", ts);
            }

            return key;
        }
    }
}
